package com.bjgov.scoresys.dao;
// default package

import static org.hibernate.criterion.Example.create;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;

import com.bjgov.scoresys.entity.UserProject;
import com.bjgov.scoresys.util.Page;

/**
 * A data access object (DAO) providing persistence and search support for
 * UserProject entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see .UserProject
 * @author MyEclipse Persistence Tools
 */

public class UserProjectDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory
			.getLog(UserProjectDAO.class);
	// property constants
	public static final String STATUS = "status";
	public static final String USER = "user";

	public int save(UserProject transientInstance) {
		log.debug("saving UserProject instance");
		int result = 0;
		try {
			result = (Integer) getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
		return result;
	}

	public void delete(UserProject persistentInstance) {
		log.debug("deleting UserProject instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public UserProject findById(java.lang.Integer id) {
		log.debug("getting UserProject instance with id: " + id);
		try {
			UserProject instance = (UserProject) getSession().get(
					"com.bjgov.scoresys.entity.UserProject", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List<UserProject> findByUserIdProjectStatus(int userId) {
		log.debug("finding UserProject instance by userIdProjectStatus");
		try {
			String queryString = "from UserProject as model where model.user.id=? and model.project.status=1";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, userId);
			return queryObject.list();			
		} catch (RuntimeException re) {
			log.error("find by userIdProjectStatus failed", re);
			throw re;
		}
	}
	
	public UserProject findByUserIdProjectId(int userId, int projectId) {
		log.debug("finding UserProject instance by userIdProjectStatus");
		try {
			String queryString = "from UserProject as model where model.user.id=? and model.project.id=?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, userId);
			queryObject.setParameter(1, projectId);
			return (UserProject) queryObject.uniqueResult();			
		} catch (RuntimeException re) {
			log.error("find by userIdProjectStatus failed", re);
			throw re;
		}
	}	

	public List<UserProject> findByExample(UserProject instance) {
		log.debug("finding UserProject instance by example");
		try {
			List<UserProject> results = (List<UserProject>) getSession()
					.createCriteria("UserProject").add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding UserProject instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from UserProject as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<UserProject> findByUser(int userID) {
		return findByProperty(USER, userID);
	}
	
	public List<UserProject> findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findAll() {
		log.debug("finding all UserProject instances");
		try {
			String queryString = "from UserProject";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public UserProject merge(UserProject detachedInstance) {
		log.debug("merging UserProject instance");
		try {
			UserProject result = (UserProject) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(UserProject instance) {
		log.debug("attaching dirty UserProject instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(UserProject instance) {
		log.debug("attaching clean UserProject instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	/**
	 * 正在进行的评分项目
	 * @param userId
	 * @param page
	 * @return
	 */
	public List getByUserPage(int userId, Page page) {
		String querySentence = "from UserProject as model where model.user.id=? and model.project.status=1 and model.status<2";
		Query query = getSession().createQuery(querySentence);
		query.setParameter(0, userId);
		query.setFirstResult(page.getBeginIndex())
		.setMaxResults(page.getEveryPage());
		return query.list();
	}
	
	/**
	 * 已完成评分的项目
	 * @param userId
	 * @param page
	 * @return
	 */
	public List getByUserCompletePage(int userId, Page page) {
		String querySentence = "from UserProject as model where model.user.id=?  and model.status=2 order by id desc";
		Query query = getSession().createQuery(querySentence);
		query.setParameter(0, userId);
		query.setFirstResult(page.getBeginIndex())
		.setMaxResults(page.getEveryPage());
		return query.list();
	}	
	
	@Override
	public List getByPage(Page page) {
		String querySentence = "from UserProject";
		Query query = getSession().createQuery(querySentence);
		query.setFirstResult(page.getBeginIndex())
		.setMaxResults(page.getEveryPage());
		return query.list();
	}

	@Override
	public int count() {
		log.debug("getUserProjectCount");
		try {
			String queryString = "select count(id) from UserProject";
			Query query = getSession().createQuery(queryString);
	        return (Integer) query.iterate().next();
		} catch (RuntimeException re) {
			log.error("getUserProjectCount failed", re);
			throw re;
		}
	}	
	
	public int countByUser(int userId) {
		log.debug("getUserProjectCount");
		try {
			String queryString = "select count(id) from UserProject as model where model.user.id=?";
			Query query = getSession().createQuery(queryString);
			query.setParameter(0, userId);
	        return (Integer) query.iterate().next();
		} catch (RuntimeException re) {
			log.error("getUserProjectCount failed", re);
			throw re;
		}
	}
	
	/**
	 * 当前正在进行的评分项目
	 * @param userId
	 * @return
	 */
	public int countByUserNowSocre(int userId) {
		log.debug("getUserProjectCount");
		try {
			String queryString = "select count(id) from UserProject as model where model.user.id=? and model.project.status=1 and model.status<2";
			Query query = getSession().createQuery(queryString);
			query.setParameter(0, userId);
	        return (Integer) query.iterate().next();
		} catch (RuntimeException re) {
			log.error("getUserProjectCount failed", re);
			throw re;
		}
	}
	
	/**
	 * 已完成评分项目
	 * @param userId
	 * @return
	 */
	public int countByUserScoreComplete(int userId) {
		log.debug("getUserProjectCount");
		try {
			String queryString = "select count(id) from UserProject as model where model.user.id=? and model.status=2";
			Query query = getSession().createQuery(queryString);
			query.setParameter(0, userId);
	        return (Integer) query.iterate().next();
		} catch (RuntimeException re) {
			log.error("getUserProjectCount failed", re);
			throw re;
		}
	}
}